
<div class="febs-container" id="febs-baseContractor-common">
    <div class="febs-component-body">
        <div class="layui-row febs-full layui-col-space8">
            <div class="layui-col-md4 layui-col-sm12 layui-col-xs12 febs-full">
                <div class="layui-card febs-full">
                    <div class="layui-card-body">
                        <form class="layui-form layui-table-form" lay-filter="baseContractorCommon-table-form" id="baseContractorCommon-table-form">
                            <div class="layui-form-item">
                                <label class="layui-form-label layui-form-label-sm" style="width:auto;">承包商名称</label>
                                <div class="layui-input-block layui-input-icon-container" style=" margin-left: 115px;">
                                    <input type="text" name="contractorName" autocomplete="off" class="layui-input">
                                    <div class="layui-input-suffix"  id="queryContractorName">
                                        <em class="layui-icon layui-icon-search"></em>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <table lay-filter="baseContractorCommonTable" id="baseContractorCommonTable"
                               lay-data="{id: 'baseContractorCommonTable'}"></table>
                    </div>
                </div>
            </div>
            <div class="layui-col-md8 layui-col-sm12 layui-col-xs12 febs-full">
                <div class="layui-card febs-full">
                    <div class="layui-card-body">
                        <form class="layui-form layui-table-form" lay-filter="baseContractorTeam-table-form" id="baseContractorTeam-table-form">
                            <div class="layui-form-item">
                                <label class="layui-form-label layui-form-label-sm" style="width: auto">承包队名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="teamName" autocomplete="off" class="layui-input">
                                </div>
                                <div id="queryTeamName" class="layui-btn">
                                    <em class="layui-icon">&#xe615;</em> 查询
                                </div>
                            </div>
                        </form>
                        <table lay-filter="baseContractorTeamCommonTable" id="baseContractorTeamCommonTable"
                               lay-data="{id: 'baseContractorTeamCommonTable'}"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
        var $ = layui.jquery,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            commonJS = layui.commonJS,
            validate = layui.validate,
            $view = $('#febs-baseContractor-common'),
            $queryContractorName = $view.find('#queryContractorName'),
            $queryTeamName = $view.find('#queryTeamName'),
            $searchForm = $view.find('#baseContractorCommon-table-form'),
            leftTableIns,
            rightTableIns;

        form.verify(validate);
        form.render();

        initTable();

        let contractorTeamTableQueryParams = {
            tenantId:null,
            contractorId:null,
            contractorName:null,
            teamName:null,
            status:null
        } ;

        var contractorInfo = {
            contractorName:"",
            contractorId:""
        }

        function initTable() {
            initLeftTable();
        }

        function initLeftTable(){
            //初始化承包商表格
            leftTableIns = febs.table.init({
                elem: $view.find('#baseContractorCommonTable'),
                id: 'baseContractorCommonTable',
                height: 'full',
                limit:10,
                skin: '',
                cols: [[
                    {type: 'numbers', title: '序号', width: 60 ,fixed:'left'},
                    {field: 'contractorCode', title: '承包方编号', width: 140,align: 'center'},
                    {field: 'contractorName', title: '承包方名称', minWidth: 120},
                    {field: 'contacts', title: '联系人', width: 115,align: 'center'},
                    {field: 'contactsPhone', title: '联系方式', width: 115,align: 'center'},
                ]],
                done: function (res, curr, count) {
                    if(res && res.count > 0 ){
                        $view.find(".layui-table-view[lay-table-id='baseContractorCommonTable'] .layui-table-body.layui-table-main tr[data-index=0]").click();
                    }else{
                        initRightTable()
                    }
                }
            });
            leftTableIns.reload({url: ctx + 'baseContractor/list', where: getQueryParams(), page: {curr: 1}});
        }

        function initRightTable(){
            //初始话承包队表
            rightTableIns = febs.table.init({
                elem: $view.find('#baseContractorTeamCommonTable'),
                id: 'baseContractorTeamCommonTable',
                skin: '',
                limit:10,
                height: 'full',
                cols: [[
                    {title: '', type: 'radio', width: 60},
                    {field: 'teamName', title: '承包队名称', minWidth: 160 },
                    {field: 'constructionDept', title: '实施部门', width: 140 ,align:'center'},
                    {field: 'teamUsersName', title: '人员明细', minWidth: 120,templet:function (d){
                            let returnText = ""
                            $.each(d.teamUserList,function (index,item){
                                if("1" == item.status){
                                    returnText += `<button class="layui-btn layui-btn-xs layui-btn-success" >${item.uname}</button>`
                                }else{
                                    returnText += `<button class="layui-btn layui-btn-xs layui-btn-disabled" disabled>${item.uname}</button>`
                                }
                            })
                            if(commonJS.isEmpty(returnText)){
                                returnText = "--"
                            }
                            return returnText;
                        }
                    },
                ]],
            });
            rightTableIns.reload({url: ctx + 'baseContractorTeam/listByContractorId', where: contractorTeamTableQueryParams, page: {curr: 1}});
        }

        table.on('row(baseContractorCommonTable)', function (obj) {
            obj.tr.addClass('layui-bg-green').siblings().removeClass('layui-bg-green');
            contractorTeamTableQueryParams.tenantId =obj.data.tenantId
            contractorTeamTableQueryParams.contractorId = obj.data.contractorId
            contractorTeamTableQueryParams.contractorName = obj.data.contractorName
            contractorTeamTableQueryParams.teamName  = $view.find('input[name="teamName"]').val();
            contractorTeamTableQueryParams.status  =  $view.find("select[name='teamStatus']").find("option:selected").val();

            contractorInfo.contractorId = obj.data.contractorId
            contractorInfo.contractorName = obj.data.contractorName

            initRightTable();
        });


        $queryContractorName.on('click', function () {
            leftTableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $queryTeamName.on('click', function () {
            rightTableIns.reload({where: queryContractorTeam(), page: {curr: 1}});
        });

        function getQueryParams() {
            return {
                contractorName: $searchForm.find('input[name="contractorName"]').val(),
                tenantId:  POPUP_DATA.tenantId,
                status: '1',
                invalidate_ie_cache: new Date()
            };
        }


        function  queryContractorTeam (obj){
            if(!commonJS.isEmpty(contractorTeamTableQueryParams.contractorId)){
                contractorTeamTableQueryParams.teamName = $view.find('input[name="teamName"]').val();
                contractorTeamTableQueryParams.status = $view.find("select[name='teamStatus']").find("option:selected").val();
                //重载右侧承包队表
                initRightTable();
            }else {
                return false;
            }
        }

        window.getSelectData = function () {
            let selData = table.checkStatus("baseContractorTeamCommonTable")
            let returnData;
            if(selData.data.length>0){
                returnData = {
                    contractorId:contractorInfo.contractorId,
                    contractorName:contractorInfo.contractorName,
                    contractorTeamId : selData.data[0].contractorTeamId,
                    contractorTeamName : selData.data[0].teamName,
                    constructionDept:selData.data[0].constructionDept,
                    constructionDeptId:selData.data[0].constructionDeptId
                }
            }
            return returnData;
        }


    });
</script>
